/**
 * 
 */
package vue.desktop;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;

import vue.popup.IJPopupMenu;
import vue.popup.JPopupPreprocessor;
import vue.session.IGPreprocessorBlock;
import controller.ISessionManager;

/**
 * Represent a preprocessor execution on the desktop.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGDeskPreprocessor extends IGDeskItem implements MouseListener {

	private static final long	serialVersionUID	= 1L;
	public static final String	DEFAULT_DESCRIPTION	= "Execution";

	private IGPreprocessorBlock	preproc;
	private JLabel				description;
	private IJPopupMenu			rightMenu;

	/**
	 * Create an {@link IGDeskPreprocessor}.
	 * 
	 * @param exec
	 *            the {@link IGPreprocessorBlock} associate
	 * @param manager
	 *            the session manager
	 */
	public IGDeskPreprocessor(IGPreprocessorBlock exec, ISessionManager manager) {
		this.preproc = exec;

		// descritpion
		description = new JLabel(DEFAULT_DESCRIPTION, JLabel.CENTER);
		add(description);

		// pop up
		rightMenu = new JPopupPreprocessor(exec, manager);

		refresh();

		addMouseListener(this);
		setSize(getPreferredSize());

	}

	/**
	 * refresh the informations.
	 */
	public void refresh() {
		description.setText(preproc.getPreproc().getPreproc().getType().getDescription());

		rightMenu.refresh();
	}

	/**
	 * Return the {@link IGPreprocessorBlock} associate.
	 * 
	 * @return the {@link IGPreprocessorBlock} associate.
	 */
	public IGPreprocessorBlock getPreproc() {
		return preproc;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		preproc.notifySelected();
	}


	@Override
	public void mouseEntered(MouseEvent e) {
	}


	@Override
	public void mouseExited(MouseEvent e) {
	}


	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}

}
